package com.zx.pm.service.impl;

import com.zx.common.utils.SecurityUtils;
import com.zx.common.utils.StringUtils;
import com.zx.pm.domain.CostCategoryDict;
import com.zx.pm.domain.Project;
import com.zx.pm.mapper.BidMapper;
import com.zx.pm.service.BidService;
import com.zx.pm.vo.ProjectVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.Collections;
import java.util.Date;
import java.util.List;
@Slf4j
@Service
public class BidServiceImpl implements BidService {
    @Resource
    private BidMapper bidMapper;

    @Override
    public List<Project> selectBidList(Project project) {
        return bidMapper.selectBidList(project);
    }

    @Override
    public boolean checkUnitNameUnique(Project project) {
        return  bidMapper.checkUnitNameUnique(project);
    }

    @Override
    public boolean checkUnitprojectNoUnique(Project project) {
        return bidMapper.checkUnitprojectNoUnique(project);
    }

    @Override
    public int insertBid(Project project) {
        // 1. 补全数据库 NOT NULL 的字段
        project.setCreateUserId(SecurityUtils.getUserId()); // 当前登录人
        project.setArchiveStatus(0);                        // 0-未存档
        // 2. 其余字段如果允许 NULL 可先不设
        return bidMapper.insertBid(project);
    }

    @Override
    @Transactional
    public void archiveProject(ProjectVo projectVo) {
        if (projectVo.getId() == null) {
            throw new IllegalArgumentException("项目ID不能为空");
        }
        if (projectVo.getArchiveStatus() == null) {
            throw new IllegalArgumentException("存档状态不能为空");
        }
        if (projectVo.getArchiveReason() == null || projectVo.getArchiveReason().trim().isEmpty()) {
            throw new IllegalArgumentException("存档原因不能为空");
        }

        //  查询项目是否存在
        Project project = bidMapper.findById(projectVo.getId())
                .orElseThrow(() -> new IllegalArgumentException("项目不存在"));

        // 设置存档信息
        project.setArchiveStatus(projectVo.getArchiveStatus());
        project.setArchiveTime(projectVo.getArchiveTime() != null ? projectVo.getArchiveTime() : new Date());
        project.setArchiveReason(projectVo.getArchiveReason().trim());

        // 更新数据库
        bidMapper.updateProjectArchiveInfo(project);

        // 记录日志
        log.info("项目存档成功：{}", project);

    }

    @Override
    public int updateBid(Project project) {
        //获得原始项目信息
        Project temp = bidMapper.selectBidById(project.getId());
        //检查项目名称是否需要变更
        if (!StringUtils.equals(temp.getProjectName(), project.getProjectName())) {

        }
        return bidMapper.updateBid(project);
    }

    @Override
    public int countBySex(int i) {
        return bidMapper.countBySex(i);
    }

    @Override
    public List<CostCategoryDict> selectEnabled() {
        return bidMapper.selectEnabled();
    }

    @Override
    public Project buildReport(Long projectId) {
        return null;
    }


}

